<?php


	include_once("Mail/mime.php");					
	
	$headers = array(
		'From'    => "Mairangi Arts Centre<mairangiartscentre@xtra.co.nz>",
		'Subject' => "Mairangi Arts Centre Purchase No. #" . $this->orderContainer->valueForKey('id'),
		'To' => $this->orderContainer->valueForKey('email'),
	);
	$mime = new Mail_mime("\n");

	$orderCurrency = $this->orderContainer->valueForKey('currency');
	$orderCurrencySign = "$";
	switch ($orderCurrency){
	
		case "NZD":
		break;
		
		case "AUD":
		break;
		
		case "GBP":
			$orderCurrencySign = "&pound;";
		break;
		
		case "EUR":
			$orderCurrencySign = "&euro;";
		break;
	
//		case "JPY":
//			$orderCurrencySign = "&yen;";
//		break;
		
		default:
		case "NZD":
		break;
	}
	
	
	ob_start();
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Mairangi Arts Centre</title>
<style type="text/css">
<!--

body {
	margin: 0;
	font: 11px "Trebuchet MS", Arial, sans-serif;
	color: #525047;
	line-height: 22px;
	color: #6d5f4d;
	}
	
	
h1 {
	color: #68510b;
	font-size: 	17px;
	line-height: normal;
	margin: 0 0 0px 0;
	}
	
a:link, a:visited {
	color: #F83A00;
	text-decoration: none;
	}
a:hover, a:active {
	text-decoration: underline;
	color: #F83A00;
	}
	
.invoiceheading {
	font-weight: bold;	
	color: #5386a1;
	font: 11px "Trebuchet MS", Arial, sans-serif;
	font-size: 15px;
	}
	
.sitetitle {
	color: #68510b;
	font-size: 12px;
	}
.divider {
	height: 12px;
	border-bottom: 1px solid #e2e2e2;
	margin-bottom: 12px;		
	}
-->
</style>
</head>
      
<body>

<font face="Trebuchet MS, Arial, sans-serif" size="2" color="#525047"><span style="font-size: 11px;">

<table width="665" border="0" align="center" cellpadding="0" cellspacing="0" >
  <tr>
    <td align="center"><img src="invoiceHead.gif" alt="Invoice" /><br /><br />

        <table width="100%" cellpadding="5" cellspacing="0">
          <tr>
            <td align="left" valign="top"><b>Mairangi Arts Centre</b><br />
              <a href="http://www.mairangiarts.co.nz/" target="_blank">www.mairangiarts.co.nz</a><br /><br />
           
              </td>
            <td colspan="-1" align="right" valign="top">
	      <b>Purchase #:</b> <?= $this->orderContainer->valueForKey('id') ?><br />
              <b>Date:</b> <?= date("d-m-Y", strtotime($this->orderContainer->valueForKey('transactionDate'))); ?>	    </td>
          </tr>
          <tr>
            <td colspan="2" align="left" valign="top">
            	<table cellpadding="0" cellspacing="0" width="100%">
                	<tr>
                    	<td width="34%" valign="top">
							<b>Postal Address:</b><br />						
							P O Box 65-121,<br/> Mairangi Bay 0754<br/>
							New Zealand<br/>				
                        </td>
                        
                        <td width="32%" valign="top">
                        <b>Ph:</b> 09 478 2237<br />
                        <b>Fax:</b> 09 478 2837<br /> 
                      <b>Email:</b> <a href="mailto:mairangiartscentre@xtra.co.nz">mairangiartscentre@xtra.co.nz</a></td>
                    </tr>
                </table>
            </td>
          </tr>
          <tr>
            <td colspan="2" align="left" valign="top"><div class="divider"> &nbsp;</div></td>
          </tr>
		  <tr>
            <td align="left" valign="top">
			  <b>To:</b> <?= $this->orderContainer->valueForKey('firstName') ?> <?= $this->orderContainer->valueForKey('lastName') ?><br />
              <b>Email:</b> <?= $this->orderContainer->valueForKey('email') ?><br />
              <b>Phone:</b> <?= $this->orderContainer->valueForKey('phone') ?><br />
		      <? if($this->orderContainer->valueForKey('mobile')){?><b>Mobile:</b> <?= $this->orderContainer->valueForKey('mobile') ?><br /><?}?>
		      <? if($this->orderContainer->valueForKey('phoneNight')){?><b>Phone Night:</b> <?= $this->orderContainer->valueForKey('phoneNight') ?><br /><? }?>
	      	  

	      
	      <br />
            </td>
            <td width="321" colspan="-1" align="left" valign="top"><b>Address: </b><br/>
    
			    <?= $this->orderContainer->valueForKey('street') ?><br />
				<? if($this->orderContainer->valueForKey('suburb') != ""){ echo $this->orderContainer->valueForKey('suburb') . "<br />"; } ?>
				
				<?= $this->orderContainer->valueForKey('city') ?><br />
		
				<? if($this->orderContainer->valueForKey('state') != ""){ echo $this->orderContainer->valueForKey('state') . "<br />"; } ?>
		
				<?= $this->orderContainer->valueForKey('zip') ?><br />
			    <?= $this->orderContainer->valueForKey('countryAsISOCode'); ?>
	    
	    	</td>
          </tr>
	  <tr>
	    <td colspan="2" align="left" valign="top">
	    
		    <? if($this->orderContainer->valueForKey('comments')){
		    ?>
		    <b>Comments: </b><br />
		    <?= nl2br($this->orderContainer->valueForKey('comments')) ?><br/>
		    <? } ?>	    
	    </td>
	  </tr>
    </table>
    
    <br />
    
    
    <br />
    
        <table width="657" border="0" cellpadding="0" cellspacing="0">
        	

        <tr bgcolor="#212121">
          <td colspan="3" align="center" bgcolor="#FFFFFF" >
		<table width="657" align="center" border="0" cellpadding="5" cellspacing="1">
		
              <tr bgcolor="#000000" >

                <td align="center" valign="top" bgcolor="#FFFFFF"><b>Image</b></td>
                <td align="left" valign="top" bgcolor="#FFFFFF"><b>Description</b></td>
                <td align="left" valign="top" bgcolor="#FFFFFF"><b>Price</b></td>
                <td align="left" valign="top" bgcolor="#FFFFFF"><b>Qty</b></td>
                <td align="right" valign="top" bgcolor="#FFFFFF"><b>Item tot.</b></td>
              </tr>

<?

	foreach ( $this->orderContainer->orderItems() as $oi){
		
		$imageData = file_get_contents(CMS_HTTP_HOST . $oi->valueForKey('productInvoiceImageUrl'));
		$imageName= null;
		
		
		if ($imageData && !is_null($imageData)) {
		//	
			$imageName = "img_" . $oi->valueForKey('id') . ".png";//png whee..
		//	
			$mime->addHTMLImage(
				$imageData,
				'image/png',
				$imageName,
				false
			);
			
		} else {
			
			$imageData = file_get_contents(CMS_HTTP_HOST . "/images/spacer.gif");
			$imageName = "img_" . $oi->valueForKey('id') . ".gif";
			
			$mime->addHTMLImage(
				$imageData,
				'image/gif',
				$imageName,
				false
			);			
		}

		
		if (is_null($oi) || $oi->valueForKey('isEmpty')) continue;

			$productId = $oi->valueForKey('productId');
			$product = $oi->getProduct();
			?>	      
              <tr bgcolor="#f9f9f9" >
                <td width="45" align="center" valign="center" bgcolor="#f9f9f9"><img src="<?= $imageName ?>" /></td>
                <td valign="center" bgcolor="#f9f9f9" align="left" ><?php echo "<strong>" . $product->title . "</strong>"; ?><?php echo $oi->valueForKey('productInvoiceDescription'); ?></td>
                <td align="right" valign="center" bgcolor="#f9f9f9"><?php echo $orderCurrencySign . sprintf("%01.2f",$oi->valueForKey('price')) ." ".$orderCurrency; ?></td>
                <td align="right" valign="center" bgcolor="#f9f9f9"><?php echo $oi->valueForKey('quantity'); ?></td>
                <td align="right" valign="center" bgcolor="#f9f9f9"><?= $orderCurrencySign . sprintf("%01.2f",$oi->valueForKey('subTotal')) ." ".$orderCurrency; ?></td>
              </tr>
			<?
		
	}
	
?>
          </table></td>
        </tr>


        <tr bgcolor="#212121">
          <td colspan="3" align="right" valign="top" bgcolor="#FFFFFF">
	    <table width="657" align="center" border="0" cellpadding="5" cellspacing="1" class="tabletext">
	    
	    	  <tr bgcolor="#212121">
                <td colspan="5" align="right" valign="top" bgcolor="#FFFFFF" ><b>Shipping Fee:</b></td>
                <td colspan="2" align="right" valign="top" bgcolor="#f9f9f9" ><?php 
				      if($this->orderContainer->valueForKey("shippingPrice") == "TBA"){
				      	echo $this->orderContainer->valueForKey("shippingPrice");
				      } else {
					      echo $orderCurrencySign;
					      printf("%01.2f",  $this->orderContainer->valueForKey('shippingPrice'));
					      echo  " " . $orderCurrency;
				      }
	      
	          ?></td>
              </tr>
              
              <tr bgcolor="#212121">
                <td colspan="5" align="right" valign="top" bgcolor="#FFFFFF" ><b>Order total:</b></td>
                <td colspan="2" align="right" valign="top" bgcolor="#f9f9f9" ><?= $orderCurrencySign . sprintf("%01.2f",$this->orderContainer->valueForKey('totalPrice')) ." ".$orderCurrency; ?></td>
              </tr>
	      
            </table>
	  </td>
        </tr>

	
    </table>
    </td>
  </tr>
  </table>
  </span>
  </font>
      <br />    
      <br />
</body>
</html>
<?

	$data = file_get_contents( CMS_HTTP_HOST . "/images/invoiceHead.gif");
	$name = "invoiceHead.gif";
	$mime->addHTMLImage(
		$data,
		"image/gif",
		$name,
		false);
	
		
	$email = ob_get_clean();
	//fucking hack
	foreach($mime->_html_images as $image) {
		if ( in_array($image['name'],array("invoiceHead.gif"))  ) {
			$email = str_replace($image['name'], "cid:".$image['cid'],$email);
			break;
		}
	}
	
	ob_start();
	//include plaintext email here..//	

	$plain = ob_get_clean();
	$plainMessage = '';
	$mime->setTxtBody( $plainMessage );	
	$mime->setHtmlBody( $email );	
	
	$body = $mime->get();	
	
	$headers = $mime->headers($headers);
			
	$params = array(
		"host" => CMS_SMTP_HOST
	);
	
	$mail =& Mail::factory('smtp', $params);
	
	
	$sent = $mail->send(array($this->orderContainer->valueForKey('email'), EMAIL_INVOICE_ADDRESS, "admin@gravitate.co.nz"), $headers, $body); // <-------
	
	//$sent = $mail->send(array($this->orderContainer->valueForKey('email'),"admin@gravitate.co.nz","mairangiartscentre@xtra.co.nz"), $headers, $body); // <-------
	//print_r($sent);
	if($sent){
		$this->orderContainer->log($this->orderContainer->id()." Email Invoice has been sent.");
		//echo "<br />invoice sent<br />";
	} else {
		$this->orderContainer->log($this->orderContainer->id()." Email Invoice has _NOT_ been sent.");
	}
	